package com.zecan.blog.service.impl;

import com.zecan.blog.job.UpdateArticleLikeJob;
import com.zecan.blog.job.UpdateArticleViewJob;
import com.zecan.blog.job.UpdateClassifyTagCountJob;
import com.zecan.blog.service.QuartzService;
import com.zecan.blog.utils.BlogConstant;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @ClassName QuartzServiceImpl
 * @Description
 * @date 2022/11/15 23:31
 * @Version 1.0
 */
@Service
@Slf4j
public class QuartzServiceImpl implements QuartzService {

    // 添加点赞触发器名 只能被调用一次
    private static final String ARTICLE_ADD_GOOD_TRIGGER_NAME = "ARTICLE_ADD_GOOD_TRIGGER_NAME";

    // 更新文章浏览量触发器名
    private static final String ARTICLE_UPDATE_VIEW_TRIGGER_NAME = "ARTICLE_UPDATE_VIEW_TRIGGER_NAME";

    // 更新标签分类数量触发器名
    private static final String CLASSIFY_TAG_TRIGGER_NAME = "CLASSIFY_TAG_TRIGGER_NAME";

    // 触发器组名
    private static final String TRIGGER_GROUP_NAME = "BLOG_TRIGGER_GROUP_NAME";
    private static final String UPDATE_ARTICLE_GOOD_CORN = "0 0/10 * * * ? ";
    private static final String UPDATE_CLASSIFY_TAG_CORN = "0 0 10,14,16 * * ?";

    private static final String UPDATE_ARTICLE_VIEW = "0 0/10 * * * ? ";

    @Resource
    private Scheduler scheduler;

//0 0 10,14,16 * * ?
    @PostConstruct
    public void init() {
        //addJob(StringUtils.concatQuartzStr(), BlogConstant.QUARTZ_ARTICLE_JOB_GROUP_NAME, "0/5 * * * * ?",null);
        addArticleGoodJob(BlogConstant.QUARTZ_ADD_GOOD_JOB_NAME, BlogConstant.QUARTZ_ARTICLE_JOB_GROUP_NAME,UPDATE_ARTICLE_GOOD_CORN);
        addUpdateClassifyTagCountJob(BlogConstant.QUARTZ_UPDATE_CLASSIFY_TAG_TOTAL,BlogConstant.QUARTZ_CLASSIFY_TAG_JOB_GROUP_NAME,UPDATE_CLASSIFY_TAG_CORN);
        addUpdateArticleViewJob(BlogConstant.QUARTZ_UPDATE_ARTICLE_VIEW, BlogConstant.QUARTZ_ARTICLE_JOB_GROUP_NAME, UPDATE_ARTICLE_VIEW);
    }

    @Override
    public void addArticleGoodJob(String jName, String jGroup, String cron) {
        try {

            // 构建JobDetail
            JobDetail jobDetail = JobBuilder.newJob(UpdateArticleLikeJob.class)
                    .withIdentity(jName, jGroup)
                    .build();
            // 按新的cronExpression表达式构建一个新的trigger
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(ARTICLE_ADD_GOOD_TRIGGER_NAME, TRIGGER_GROUP_NAME)
                    .startNow()
                    .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                    .build();
                scheduler.start();
                scheduler.scheduleJob(jobDetail, trigger);

        } catch (Exception e) {
            log.info("创建定时任务失败" + e);
        }

    }

    @Override
    public void addUpdateClassifyTagCountJob(String jName, String jGroup, String cron) {
        try {

            // 构建JobDetail
            JobDetail jobDetail = JobBuilder.newJob(UpdateClassifyTagCountJob.class)
                    .withIdentity(jName, jGroup)
                    .build();
            // 按新的cronExpression表达式构建一个新的trigger
            CronTrigger trigger = TriggerBuilder.newTrigger()
                    .withIdentity(CLASSIFY_TAG_TRIGGER_NAME, TRIGGER_GROUP_NAME)
                    .startNow()
                    .withSchedule(CronScheduleBuilder.cronSchedule(cron))
                    .build();
            scheduler.start();
            scheduler.scheduleJob(jobDetail, trigger);

        } catch (Exception e) {
            log.info("创建定时任务失败" + e);
        }
    }

    @Override
    public void addUpdateArticleViewJob(String jName, String jGroup, String cron) {
        try {
            JobDetail jobDetail = JobBuilder.newJob(UpdateArticleViewJob.class).withIdentity(jName, jGroup).build();
            CronTrigger trigger = (CronTrigger)TriggerBuilder.newTrigger().
                    withIdentity(ARTICLE_UPDATE_VIEW_TRIGGER_NAME, TRIGGER_GROUP_NAME).
                    startNow().withSchedule(CronScheduleBuilder.cronSchedule(cron)).build();
            this.scheduler.start();
            this.scheduler.scheduleJob(jobDetail, trigger);
        } catch (Exception var6) {
            log.info("创建定时任务失败" + var6);
        }
    }

    @Override
    public void pauseJob(String jName, String jGroup) throws SchedulerException {
        scheduler.pauseJob(JobKey.jobKey(jName, jGroup));
    }

    @Override
    public void resumeJob(String jName, String jGroup) throws SchedulerException {
        scheduler.resumeJob(JobKey.jobKey(jName, jGroup));
    }

    @Override
    public void rescheduleJob(String jName, String jGroup, String cron) throws SchedulerException {
        TriggerKey triggerKey = TriggerKey.triggerKey(jName, jGroup);
        // 表达式调度构建器
        CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cron);
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        // 按新的cronExpression表达式重新构建trigger
        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
        // 按新的trigger重新设置job执行，重启触发器
        scheduler.rescheduleJob(triggerKey, trigger);
    }

    @Override
    public void deleteJob(String jName, String jGroup) throws SchedulerException {
        scheduler.pauseTrigger(TriggerKey.triggerKey(jName, jGroup));
        scheduler.unscheduleJob(TriggerKey.triggerKey(jName, jGroup));
        scheduler.deleteJob(JobKey.jobKey(jName, jGroup));
    }

}
